package com.gene.controller.cart;


import com.gene.pojo.UserCart;
import com.gene.pojo.entity.CartEntity;
import com.gene.service.IUserCartService;
import com.gene.utils.ResultData;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 购物车
 */
@Controller
@RequestMapping("/gene-safe/userCart")
public class UserCartController {
    @Autowired
    private IUserCartService userCartService;

    /**
     * 查询   查询条件为userId(用户id)
     * @param userCart
     * @return
     */
    @RequestMapping(value="/query")
    @ResponseBody
    public ResultData query(UserCart userCart){
        ResultData result=new ResultData();
        List<CartEntity> list=userCartService.query(userCart);
        if(list.size()>0){
            result.setData(list);
            result.setStatus("1");
            result.setMsg("查询成功！");
        }else{
            result.setStatus("0");
            result.setMsg("没有数据！");
        }

        return result;
    }


    /**
     * 查询   查询条件为cartId
     * @param userCart
     * @return
     */
    @RequestMapping(value="/selectId")
    @ResponseBody
    public ResultData selectId(UserCart userCart){
        ResultData result=new ResultData();
        UserCart u = userCartService.selectId(userCart);
        if(null != u){
            result.setData(u);
            result.setStatus("1");
            result.setMsg("查询成功！");
        }else{
            result.setStatus("0");
            result.setMsg("没有数据！");
        }

        return result;
    }



    /**
     * 新增
     * @param userCart
     * @return
     */
    @RequestMapping(value="/add")
    @ResponseBody
    public ResultData add(UserCart userCart){
        ResultData result =new ResultData();
        userCart.setCreateTime(new Date());
        boolean a=userCartService.add(userCart);
        if(a){
            result.setStatus("1");
            result.setMsg("新增成功...");
        }else{
            result.setStatus("0");
            result.setMsg("新增失败！");
        }
        return result;
    }


    /**
     * 修改
     *
     * @param userCart
     * @return
     */
    @RequestMapping(value="/edit")
    @ResponseBody
    public ResultData edit(UserCart userCart){
        ResultData result =new ResultData();
        userCart.setUpdateTime(new Date());
        boolean a=userCartService.edit(userCart);
        if(a){
            result.setStatus("1");
            result.setMsg("修改成功...");
        }else{
            result.setStatus("0");
            result.setMsg("修改失败！");
        }
        return result;
    }


    /**
     * 批量修改
     *
     * @param list
     * @return
     */
    @RequestMapping(value="/editList")
    @ResponseBody
    public ResultData editList(@RequestParam(value = "list",defaultValue = "1", required = true)String list){
        ResultData result =new ResultData();

        List<UserCart> l = new ArrayList<UserCart>();
        JSONArray jsonArray = JSONArray.fromObject(list);//把String转换为json
        l = (List<UserCart>) JSONArray.toCollection(jsonArray,UserCart.class);
        boolean a=userCartService.editList(l);

        if(a){
            result.setStatus("1");
            result.setMsg("修改成功...");
        }else{
            result.setStatus("0");
            result.setMsg("修改失败！");
        }
        return result;
    }


    /**
     * 删除
     * @param userCart
     * @return
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    public ResultData delete(UserCart userCart) {
        ResultData result = new ResultData();
        boolean a=userCartService.delete(userCart);
        if(a){
            result.setStatus("1");
            result.setMsg("删除成功...");
        }else{
            result.setStatus("0");
            result.setMsg("删除失败！");
        }
        return result;
    }



    /**
     * 批量删除
     * @param userCart
     * @return
     */
    @RequestMapping(value="/deleteList")
    @ResponseBody
    public ResultData deleteList(UserCart userCart) {
        ResultData result = new ResultData();
        boolean a=userCartService.deleteList(userCart);
        if(a){
            result.setStatus("1");
            result.setMsg("批量删除成功...");
        }else{
            result.setStatus("0");
            result.setMsg("批量删除失败！");
        }
        return result;
    }
}
